package com.ykun.springboot.service;

import com.github.pagehelper.PageHelper;
import com.ykun.springboot.mapper.CityMapper;
import com.ykun.springboot.model.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by yangkun on 2017/7/30.
 */
@Service
public class CityService {

    @Autowired
    private CityMapper cityMapper;

    public List<City> getAll(City city) {
        if(city.getPage() != null && city.getRows() != null){
            PageHelper.startPage(city.getPage(), city.getRows());
        }
        return cityMapper.selectAll();
    }

    public City getById(Integer id) {
        return cityMapper.selectByPrimaryKey(id);
    }

    public void deleteById(Integer id) {
        cityMapper.deleteByPrimaryKey(id);
    }

    public void saveOrUpdate(City city) {
        if(city.getId() == null){
            cityMapper.insert(city);
        }else{
            cityMapper.updateByPrimaryKey(city);
        }
    }
}
